<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/5/6
 * Time: 16:02
 * Desc: 微信H5支付
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use weixinpaymobile\Weixinpaymobile;
/**
 * 微信支付
 */
header("Content-type:text/html;charset=utf-8");
class Wxpaymobile extends Controller
{
    /**
     * @Author  lingyun
     * @Desc    微信H5支付
     */
    public function h5Pay_order(){
        $root = request()->domain();
        $order_sn = input('order_sn');          //订单信息
//        $order_sn = '20180506170715957293000001';
        $this->check_order($order_sn);
        $order = db('order')->where('order_sn',$order_sn)->find();          //订单信息

        //配置信息
        $appid = db('system_config')->where('name','wechat_appid')->value('value');
        $mechid = db('system_config')->where('name','wechat_mch_id')->value('value');
        $key = db('system_config')->where('name','wechat_partnerkey')->value('value');

        $notify_url             = $root.'/index.php/index/notifywxpaymobile/index?out_trade_no='.$order_sn;  //拼接回调路径，带单号方便回调
        $wechatAppPay           = new Weixinpaymobile($appid, $mechid, $notify_url, $key);
        $params['body']         = '微信支付';                       //商品描述
        $params['out_trade_no'] = $order_sn;    //自定义的订单号
        $params['total_fee']    = $order['final_amount']*100;                       //订单金额 只能为整数 单位为分
        $params['trade_type']   = 'MWEB';                   //交易类型 JSAPI | NATIVE | APP | WAP
        $params['scene_info']   = '{"h5_info": {"type":"Wap","wap_url": "https://ysbl.fxshe.com","wap_name": "支付中"}}';
        $result                 = $wechatAppPay->unifiedOrder($params);//统一下单方法


        $url_encode_redirect_url = urlencode($root.'/index.php/index/pay_complete/get_coupon?out_trade_no='.$order_sn);//支付成功回调路径，在Home/config.php文件中可配置
        $url                     = $result['mweb_url'] . '&redirect_url=' . $url_encode_redirect_url;//拼接支付链接
        echo "<script>window.location.href='" . $url . "';</script>";
        exit();
    }

    /**
     * @Author  lingyun
     * @Desc    检查订单信息
     */
    private function check_order($order_sn){
        $order = Db::table("ys_order")->where("order_sn",$order_sn)->find();
        if(empty($order)){
            $this->assign('err_msg','找不该订单信息，请重新下单');
            $this->assign('err_url',url('GoodsList/goods_list'));
            $this->assign('err_button','重新下单');
            return $this->fetch('public/error');
            exit();
        }
        if($order["order_status"]!=0){
            $this->assign('err_msg','该订单已经支付，请勿重复支付');
            $this->assign('err_url',url('GoodsList/goods_list'));
            $this->assign('err_button','浏览其它商品');
            return $this->fetch('public/error');
            exit();
        }
        if(time()-3600>$order["addtime"]){
            $this->assign('err_msg','该订单已过期，请浏览其它商品');
            $this->assign('err_url',url('GoodsList/goods_list'));
            $this->assign('err_button','浏览其它商品');
            return $this->fetch('public/error');
            exit();
        }
        return ["status"=>1,"data"=>'可以支付'];
    }

    /**
     * @Author  lingyun
     * @Desc    h5支付代金券
     * 2018-05-06
     */
    public function h5pay_cash_coupon(){
        $root = request()->domain();
        $post = input('param.');
        $uid = session('_user_uid_.uid');
        $coupon = db('store_coupon')->where('id',$post['id'])->find();
        $data_arr = [
            'order_sn'=>create_order_sn($uid),
            'uid'=>$uid,
            'cash_coupon_id'=>$post['id'],
            'store_id'=>session('store_id'),
            'pay_time'=>time(),
            'pay_type'=>$post['pay_type'],
            'price'=>$coupon['price']*100,
            'addtime'=>time(),
            'remark'=>'待支付',
        ];
        //插入支付订单
        $res = db('cash_coupon_list')->insert($data_arr);

        //配置信息
        $appid = db('system_config')->where('name','wechat_appid')->value('value');
        $mechid = db('system_config')->where('name','wechat_mch_id')->value('value');
        $key = db('system_config')->where('name','wechat_partnerkey')->value('value');

        $notify_url             = $root.'/index.php/index/notifywxpaymobile/cash_coupon?out_trade_no='.$data_arr['order_sn'];  //拼接回调路径，带单号方便回调
        $wechatAppPay           = new Weixinpaymobile($appid, $mechid, $notify_url, $key);
        $params['body']         = '微信支付';                       //商品描述
        $params['out_trade_no'] = $data_arr['order_sn'];    //自定义的订单号
        $params['total_fee']    = $coupon['price']*100;                       //订单金额 只能为整数 单位为分
        $params['trade_type']   = 'MWEB';                   //交易类型 JSAPI | NATIVE | APP | WAP
        $params['scene_info']   = '{"h5_info": {"type":"Wap","wap_url": "https://ysbl.fxshe.com","wap_name": "支付中"}}';
        $result                 = $wechatAppPay->unifiedOrder($params);//统一下单方法

        $url_encode_redirect_url = urlencode($root.'/index.php/index/goods_list/goods_list');//支付成功回调路径，在Home/config.php文件中可配置
        $url                     = $result['mweb_url'] . '&redirect_url=' . $url_encode_redirect_url;//拼接支付链接
        echo "<script>window.location.href='" . $url . "';</script>";
        exit();
    }

    /**
     * @Author  lingyun
     * @Desc    h5支付会员充值
     * 2018-05-06
     */
    public function h5pay_recharge(){
        $root = request()->domain();
        $order_sn = input('order_sn');          //订单信息
        $this->check_recharge_order($order_sn);
        $order = db('user_wallet_log')->where('order_sn',$order_sn)->find();          //订单信息

        //配置信息
        $appid = db('system_config')->where('name','wechat_appid')->value('value');
        $mechid = db('system_config')->where('name','wechat_mch_id')->value('value');
        $key = db('system_config')->where('name','wechat_partnerkey')->value('value');

        $notify_url             = $root.'/index.php/index/notifywxpaymobile/recharge?out_trade_no='.$order_sn;  //拼接回调路径，带单号方便回调
        $wechatAppPay           = new Weixinpaymobile($appid, $mechid, $notify_url, $key);
        $params['body']         = '微信支付';                       //商品描述
        $params['out_trade_no'] = $order_sn;    //自定义的订单号
        $params['total_fee']    = $order['amount']*100;                       //订单金额 只能为整数 单位为分
        $params['trade_type']   = 'MWEB';                   //交易类型 JSAPI | NATIVE | APP | WAP
        $params['scene_info']   = '{"h5_info": {"type":"Wap","wap_url": "https://ysbl.fxshe.com","wap_name": "支付中"}}';
        $result                 = $wechatAppPay->unifiedOrder($params);//统一下单方法


        $url_encode_redirect_url = urlencode($root.'/index.php/index/user/recharge?out_trade_no='.$order_sn);//支付成功回调路径，在Home/config.php文件中可配置
        $url                     = $result['mweb_url'] . '&redirect_url=' . $url_encode_redirect_url;//拼接支付链接
        echo "<script>window.location.href='" . $url . "';</script>";
        exit();
    }

    /**
     * @Author  lingyun
     * @Desc    检查充值订单
     * @param $order_sn
     * @return array|mixed
     */
    private function check_recharge_order($order_sn){
        if(empty($order_sn)){
            $this->assign('err_msg','找不到订单信息，请重新充值');
            $this->assign('err_url',url('User/recharge'));
            $this->assign('err_button','重新充值');
            return $this->fetch('public/error');
            exit();
        }else{
            $order = Db::name('user_wallet_log')->where("order_sn",$order_sn)->find();
            if(empty($order)){
                $this->assign('err_msg','找不到订单信息，请重新充值');
                $this->assign('err_url',url('User/recharge'));
                $this->assign('err_button','重新充值');
                return $this->fetch('public/error');
                exit();
            }else if($order['pay_status'] == 1){
                $this->assign('err_msg','该订单已支付，请勿重复支付');
                $this->assign('err_url',url('User/recharge'));
                $this->assign('err_button','重新充值');
                return $this->fetch('public/error');
                exit();
            }else if(time()-3600>$order["addtime"]){
                $this->assign('err_msg','该订单已过期，请重新下单');
                $this->assign('err_url',url('User/recharge'));
                $this->assign('err_button','重新充值');
                return $this->fetch('public/error');
                exit();
            }
        }
        return ['status'=>1,'msg'=>'找到订单'];
    }



}